<?php
session_start();
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$searchid = isset($_POST['searchid']) ? mysql_real_escape_string($_POST['searchid']) : '';
$offset = ($page - 1) * $rows;
$result = array();

include 'conn.php';
if (!empty($_SESSION["userType"])) {
    $userType = $_SESSION["userType"];
}
if (!empty($_SESSION["centreName"])) {
    $centreName = $_SESSION["centreName"];
}
if ($userType == "System Admin") {
    $rs = mysql_query("SELECT COUNT( * ) 
FROM user, centre
WHERE user.CentreId IS NOT NULL 
AND user.CentreId = centre.CentreId
AND (
Username LIKE  '%$searchid%'
OR UserType LIKE  '%$searchid%'
OR StaffName LIKE  '%$searchid%'
OR ContactNumber LIKE  '%$searchid%'
OR CentreName LIKE  '%$searchid%'
OR Email LIKE  '%$searchid%'
)");
    $row = mysql_fetch_row($rs);
    $result["total"] = $row[0];
    $rs = mysql_query("SELECT Username, UserType, StaffName, ContactNumber, Email, CentreName
FROM user, centre
WHERE user.CentreId IS NOT NULL 
AND user.CentreId = centre.CentreId
AND (
Username LIKE  '%$searchid%'
OR UserType LIKE  '%$searchid%'
OR StaffName LIKE  '%$searchid%'
OR ContactNumber LIKE  '%$searchid%'
OR CentreName LIKE  '%$searchid%'
OR Email LIKE  '%$searchid%'
) limit $offset,$rows");

    $items = array();
    while ($row = mysql_fetch_object($rs)) {
        array_push($items, $row);
    }

    $rs1 = mysql_query("SELECT COUNT( * ) 
FROM user
WHERE CentreId IS NULL 
AND (
Username LIKE  '%$searchid%'
OR UserType LIKE  '%$searchid%'
OR StaffName LIKE  '%$searchid%'
OR ContactNumber LIKE  '%$searchid%'
OR Email LIKE  '%$searchid%'
)");
    $row1 = mysql_fetch_row($rs1);
    $result1["total"] = $row1[0];
    $rs1 = mysql_query("SELECT Username, UserType, StaffName, ContactNumber, Email,  ' ' AS CentreName
FROM user
WHERE CentreId IS NULL 
AND (
Username LIKE  '%$searchid%'
OR UserType LIKE  '%$searchid%'
OR StaffName LIKE  '%$searchid%'
OR ContactNumber LIKE  '%$searchid%'
OR Email LIKE  '%$searchid%'
) limit $offset,$rows");


    $items1 = array();
    while ($row1 = mysql_fetch_object($rs1)) {
        array_push($items1, $row1);
    }

    $result["rows"] = array_merge($items, $items1);
    echo json_encode($result);
} else {

    $rs = mysql_query("SELECT COUNT( * ) 
FROM user, centre
WHERE centre.CentreName ='$centreName'
AND user.CentreId = centre.CentreId
AND (
Username LIKE  '%$searchid%'
OR UserType LIKE  '%$searchid%'
OR StaffName LIKE  '%$searchid%'
OR ContactNumber LIKE  '%$searchid%'
OR Email LIKE  '%$searchid%'
)");
    $row = mysql_fetch_row($rs);
    $result["total"] = $row[0];
    $rs = mysql_query("SELECT Username, UserType, StaffName, ContactNumber, Email, CentreName
FROM user, centre
WHERE centre.CentreName ='$centreName'
AND user.CentreId = centre.CentreId
AND (
Username LIKE  '%$searchid%'
OR UserType LIKE  '%$searchid%'
OR StaffName LIKE  '%$searchid%'
OR ContactNumber LIKE  '%$searchid%'
OR Email LIKE  '%$searchid%'
) limit $offset,$rows");

    $items = array();
    while ($row = mysql_fetch_object($rs)) {
        array_push($items, $row);
    }
    $result["rows"] = $items;
    echo json_encode($result);
}
?>